package my.sweep_tchap;
/**
 *
 * @author Tyler Chapman
 * @version 0.1
 * 
 * The next page the user will see after the map page. Makes a call to the database
 * to find the nearest four streets from the user's current location. 
 * The callback sends these streets and the user can select the street and
 * direction their car is facing. The user can also press a button to go 
 * to the streetInput page and input their exact location. This allows 
 * the user to find a location that has not been served up by the street database.
 * When the user confirms their location the street code corresponding to that location
 * is checked and the user is sent to either the AlarmPage or the error page (in the
 * case of an invalid code).
 * 
 */

import java.util.ArrayList;
import org.xmlvm.iphone.CGRect;
import org.xmlvm.iphone.NSIndexPath;
import org.xmlvm.iphone.UIButton;
import org.xmlvm.iphone.UIButtonType;
import org.xmlvm.iphone.UIColor;
import org.xmlvm.iphone.UIControl;
import org.xmlvm.iphone.UIControlDelegate;
import org.xmlvm.iphone.UIControlEvent;
import org.xmlvm.iphone.UIControlState;
import org.xmlvm.iphone.UILabel;
import org.xmlvm.iphone.UITableView;
import org.xmlvm.iphone.UITableViewCell;
import org.xmlvm.iphone.UITableViewController;
import org.xmlvm.iphone.UITableViewDataSource;
import org.xmlvm.iphone.UITableViewDelegate;
import org.xmlvm.iphone.UITableViewStyle;
import org.xmlvm.iphone.UIView;
import org.xmlvm.iphone.UIViewController;

public class StreetPage extends UIViewController {
	private double latitude; 
	private double longitude;
	private int streetCode;
	private ArrayList<String> streetList = new ArrayList<String>();
	private ArrayList<Integer> codeList = new ArrayList<Integer>();
    
	public StreetPage(double lati, double longi){
		this.latitude = lati;
		this.longitude = longi;
		this.fillMap();
	}
	
    private String getItem(int row) {
        return streetList.get(row);
    }
    
    public void fillMap(){
    	/**
    	 * Connects to the street sweeping database provided by the city.
    	 * Fills ArrayLists of both the names of the nearest streets, and the index codes 
    	 * of those street in the database.
    	 */
    	connect dbConnection = new connect();
    	dbConnection.fillMap(longitude, latitude);
    	streetList = dbConnection.getStreetList();
    	codeList = dbConnection.getCodeList();
    }
    
    class DataSource extends UITableViewDataSource {
        
        @Override
        public String titleForHeaderInSection(UITableView table, int section) {
            return "Street Options";
        }
        
        @Override
        public int numberOfRowsInSection(UITableView table, int section) {
            /**
             * Sets how many streets will be shown for the user to select. This is
             * currently set to 4.
             */
        	return streetList.size();
        }
        
        @Override
        public UITableViewCell cellForRowAtIndexPath(UITableView table, NSIndexPath idx) {
            /**
             * This method will be invoked whenever the UITableView requests
             * data for a particular cell. The cell is specified via row.
             * The UITableViewCell whose label corresponds with the appropriate 
             * street name is returned.
             */
            UITableViewCell cell = new UITableViewCell();
            UILabel label = cell.getTextLabel();
            label.setText(getItem(idx.getRow()));
            return cell;
        }
        
    }
    
    class Delegate extends UITableViewDelegate {
        @Override
        public void didSelectRowAtIndexPath(UITableView tableview, NSIndexPath idx) {
        	/**
        	 * This method will be called whenever the user selects a
        	 * particular row in the table. The specific row is given as the
        	 * index of its row.
        	 */
        	String item = getItem(idx.getRow());     	
        	if(streetList.contains(item)){
        		streetCode = codeList.get(streetList.indexOf(item));
        		if(streetCode == 0){
        			/* 
                     * 0 is an invalid street code signifying that there is bad data
                     * for the street selected. This will send the user to the ErrorPage and
                     * push an errorPage onto the NavController stack
                     */
        			getNavigationController().pushViewController(new ErrorPage(), true);
        		}else{
        			/* 
        			 * If a correct street code is selected the user is sent to the AlarmPage
        			 * and an AlarmPage is pushed onto the NavController stack
        			 */
        			getNavigationController().pushViewController(new AlarmPage(streetCode), true);
        		}
        	}
        }
    };
	
	public void loadView() {
       	super.loadView();
       	UIView root = getView();
       	root.setBackgroundColor(UIColor.groupTableViewBackgroundColor);
       	
       	UITableViewController tableControl = new UITableViewController(UITableViewStyle.Grouped);
       	UITableView table = new UITableView();
       	table = tableControl.getTableView();
       	table.setDataSource(new DataSource());
       	table.setDelegate(new Delegate());
       	this.setTitle("Street");
       	root.addSubview(table);
        
        UIButton button = UIButton.buttonWithType(UIButtonType.RoundedRect);
        button.setFrame(new CGRect(40, 370, 240, 30));
        button.setTitle("Input Other Location", UIControlState.Normal);
        button.addTarget(new UIControlDelegate() {
        	/*
        	 * Invoked when the user selects the button, and elects to put in their own street.
        	 * A StreetInput page is pushed onto the NavController stack 
        	 */
            public void raiseEvent(UIControl uic, int i) {
                getNavigationController().pushViewController(new StreetInput(), true);
            }
        }, UIControlEvent.TouchUpInside);
        root.addSubview(button);
        
        setView(root);
	}
    
}


